package com.bnyy.medicalHousekeeper;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.bnyy.bean.event.Event;
import com.bnyy.common.Constant;
import com.bnyy.common.ResponseData;
import com.bnyy.common.bean.UserInfo;
import com.bnyy.medicalHousekeeper.bean.User;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/1/23
 **/
public class UserInfoManager {

    private static final int ORIGINAL_SELECTED_USERID = 0;

    private static final String TAG = "UserInfoManager";
    private User user;
    private static UserInfoManager userInfoManager;

    private boolean bindNewDevice = false;

    /**
     * 首页选中的用户id
     */
    private int selectedUserId = ORIGINAL_SELECTED_USERID;
    private Context context;
    private boolean noDevice = false;

    public void init(Context context) {
        if (context instanceof Application) {
            this.context = context;
        }
    }

    private UserInfoManager() {

    }

    public static UserInfoManager getInstance() {
        if (userInfoManager == null) {
            userInfoManager = new UserInfoManager();
        }
        return userInfoManager;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void logout() {
        user = null;
        selectedUserId = ORIGINAL_SELECTED_USERID;
        SharedPreferences sp = context.getSharedPreferences(Constant.SP_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(Constant.SpTag.TOKEN)
                .remove(Constant.SpTag.LATEST_USER_ID)
                .apply();
    }

    public int getSelectedUserId() {
        return selectedUserId;
    }


    public User getLoginUser() {
        return user;
    }

    public int getLoginUserId() {
        UserInfo userInfo = getLoginUserInfo();
        return userInfo == null ? 0 : userInfo.getId();
    }

    public int getLoginUserRoleId() {
        UserInfo userInfo = getLoginUserInfo();
        return userInfo == null ? 0 : userInfo.getRole_id();
    }

    public UserInfo getLoginUserInfo() {
        if (user != null) {
            if (user.getUserInfo() != null) {
                return user.getUserInfo();
            }
        }
        return null;
    }

}





















